package cn.iocoder.yudao.module.resource.service.cosplayoperation.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.api.enums.EnumAipError;
import cn.iocoder.yudao.module.resource.controller.admin.cosplayoperation.CosplayOperationPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.cosplayoperation.CosplayOperationSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.cosplayoperation.CosplayOperationDO;
import cn.iocoder.yudao.module.resource.dal.mysql.cosplayoperation.CosplayOperationMapper;
import cn.iocoder.yudao.module.resource.service.cosplayoperation.CosplayOperationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 角色操作 Service 实现类
 *
 * @author lizhongwen
 */
@Service
@Validated
public class CosplayOperationServiceImpl implements CosplayOperationService {

    @Resource
    private CosplayOperationMapper cosplayOperationMapper;

    @Override
    public Long createCosplayOperation(CosplayOperationSaveReqVO createReqVO) {
        // 插入
        CosplayOperationDO cosplayOperation = BeanUtils.toBean(createReqVO, CosplayOperationDO.class);
        cosplayOperationMapper.insert(cosplayOperation);
        // 返回
        return cosplayOperation.getId();
    }

    @Override
    public void updateCosplayOperation(CosplayOperationSaveReqVO updateReqVO) {
        // 校验存在
        validateCosplayOperationExists(updateReqVO.getId());
        // 更新
        CosplayOperationDO updateObj = BeanUtils.toBean(updateReqVO, CosplayOperationDO.class);
        cosplayOperationMapper.updateById(updateObj);
    }

    @Override
    public void deleteCosplayOperation(Long id) {
        // 校验存在
        validateCosplayOperationExists(id);
        // 删除
        cosplayOperationMapper.deleteById(id);
    }

    private void validateCosplayOperationExists(Long id) {
        if (cosplayOperationMapper.selectById(id) == null) {
            throw exception(EnumAipError.COSPLAY_OPERATION_NOT_EXISTS);
        }
    }

    @Override
    public CosplayOperationDO getCosplayOperation(Long id) {
        return cosplayOperationMapper.selectById(id);
    }

    @Override
    public CosplayOperationDO getCosplayOperationByRoleId(Long userId, Integer roleId) {
        return cosplayOperationMapper.selectOne(CosplayOperationDO::getUserId, userId, CosplayOperationDO::getRoleId, roleId);
    }

    @Override
    public PageResult<CosplayOperationDO> getCosplayOperationPage(Long userId, CosplayOperationPageReqVO pageReqVO) {
        return cosplayOperationMapper.selectPage(userId, pageReqVO);
    }

}